﻿using DigitalHealth.PACS.ChatWebSocket.Services;
using DigitalHealth.PACS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace DigitalHealth.PACS.ChatWebSocket.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    //[Authorize]
    public class ChatGroupController : ControllerBase
    {
        public IChatGroupServer chatGroupServer { get; set; }
        public IChatUserServer chatUserServer { get; set; }


        public ChatGroupController(IChatGroupServer chatGroupServer, IChatUserServer chatUserServer) 
        {
            this.chatUserServer = chatUserServer;
            this.chatGroupServer = chatGroupServer;
        }

        // GET: api/<ChatGroupController>
        [HttpGet]
        public IEnumerable<ChatGroup> Get()
        {
            return chatGroupServer.GetChatGroups();
        }

        // GET api/<ChatGroupController>/5
        [HttpGet("{id}")]
        public ChatGroup GetById(string id)
        {
            return chatGroupServer.GetChatGroupById(id);
        }

        [HttpGet("user/{userid}")]
        public IEnumerable<ChatGroup> GetByUserId(string userid)
        {
            ChatUser chatUser = default;
            if (string.IsNullOrWhiteSpace(userid))
                chatUser = chatUserServer.GetChatUsersById(userid);

            if (chatUser != null)
                return chatGroupServer.GetChatGroupByUser(chatUser);
            else
                return null;
        }

        [HttpGet("user/userno/{userno}")]
        public IEnumerable<ChatGroup> GetByUserNo(string userno)
        {
            ChatUser chatUser = default;
            if (string.IsNullOrWhiteSpace(userno))
                chatUser = chatUserServer.GetChatUsersByNo(userno);

            if (chatUser != null)
                return chatGroupServer.GetChatGroupByUser(chatUser);
            else
                return null;
        }

        // POST api/<ChatGroupController>
        [HttpPost]
        public void Post(ChatGroup chatGroup)
        {
            chatGroupServer.Update(chatGroup);
        }

        // PUT api/<ChatGroupController>/5
        [HttpPut()]
        public void Put([FromBody] ChatGroup chatGroup)
        {
            chatGroupServer.Create(chatGroup);
        }

        // DELETE api/<ChatGroupController>/5
        [HttpDelete()]
        public void Delete([FromBody] ChatGroup chatGroup)
        {
            chatGroupServer.Delete(chatGroup);
        }
    }
}
